"""
@File    ：page_login_h5.py
@Author  ：ixiaodu
@Date    ：2024/5/14
@Desc    ：
"""
import allure
import requests

from config.read_cfg import smtp_cfg
from page_element import el_login_h5
from utils.timer import sleep
from common.page_base import Base


class PageLoginH5(Base):
    """登录页面"""

    def page_input_username(self, phone):
        """输入登录手机号"""
        self.base_input(el_login_h5.login_username, phone)
        sleep()

    def page_input_verify_code(self, verify_code):
        """输入验证码"""
        self.base_input(el_login_h5.login_code, verify_code)

    def page_click_login_btn(self):
        """点击登录按钮"""
        self.base_click(el_login_h5.login_btn)
        sleep()

    def click_login_btn_notarize(self):
        """点击确认协议按钮"""
        self.base_click(el_login_h5.login_btn_notarize)

    def click_tab_admin(self):
        """点击管理员登录"""
        self.base_click(el_login_h5.tab_admin)
        sleep()

    def input_admin_user(self, user):
        """输入管理员账号"""
        self.base_input(el_login_h5.admin_user, user)
        sleep()

    def input_admin_password(self, password):
        """输入管理员密码"""
        self.base_input(el_login_h5.admin_password, password)
        sleep()

    def click_admin_login_btn(self):
        """点击管理员登录按钮"""
        self.base_click(el_login_h5.admin_login_btn)
        sleep()

    def page_get_error_info(self):
        """获取异常提示信息"""
        pass

    def get_login_verify_code(self, phone):
        """获取登录验证码方法"""
        url = f'{smtp_cfg["host"]}/api/authq/sms/sendLoginValidCode'
        json = {"phone": phone}
        res = requests.post(url=url, json=json)
        sleep(3)
        verify_code = res.json()['msg']
        return verify_code

    def page_login_h5(self, phone):
        """手机号登录方法"""
        sleep(1)
        with allure.step(f"输入手机号: {phone}"):
            self.page_input_username(phone)
        with allure.step("获取验证码"):
            self.get_login_verify_code(phone)
        with allure.step(f"输入验证码: {self.get_login_verify_code(phone)}"):
            self.page_input_verify_code(self.get_login_verify_code(phone))
        sleep(1)
        with allure.step("点击登录按钮"):
            self.page_click_login_btn()
        with allure.step("点击确认协议按钮"):
            self.click_login_btn_notarize()
        sleep(5)

    def page_login_h5_admin(self, user, password):
        """管理员登录方法"""
        sleep()
        with allure.step(f"点击管理员登录"):
            self.click_tab_admin()
        with allure.step(f"输入管理员账号: {user}"):
            self.input_admin_user(user)
        with allure.step(f"输入管理员密码: {password}"):
            self.input_admin_password(password)
        sleep()
        with allure.step("点击登录按钮"):
            self.click_admin_login_btn()
        with allure.step("点击确认协议按钮"):
            self.click_login_btn_notarize()
        sleep(5)
